<!--报修管理页-->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">报修管理</span>
   <el-button @click="dialogComplainVisible=true" type="primary" style="float:right;margin-top:13px;">新建报修</el-button>
  </div>
  <!-- 新增报修弹窗 -->
 <el-dialog :title="dialogTitle" v-model="dialogComplainVisible" style="width:700px;padding:40px;">
      <el-form label-width="80px" label-position="top">
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="关联房屋">
              <el-input placeholder="请输入内容" v-model="search.rhouse"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="维修项目">
              <el-input placeholder="请输入内容" v-model="search.rproject"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="问题描述">
              <el-input placeholder="请输入" type="textarea" resize="none" :rows="3" v-model="search.rtitle"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="报修用户">
             <el-select placeholder="请选择" v-model="search.rlid">
                <el-option v-for="item in repairerOptions" :label="item.lname" :value="item.lid"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="预约日期">
              <el-date-picker type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" v-model="search.rtime"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="维修状态">
              <el-select placeholder="请选择" v-model="search.rstate">
                <el-option v-for="item in repairStatusOptions" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="30">
          <el-col :span="24">
            <el-form-item label="问题附件">
              <!--  图片上传开始  -->
              <el-upload
                  v-model:file-list="fileList"
                  action="http://localhost:8080/v1/file/upload"
                  name="file"
                  limit="1"
                  list-type="picture-card"
                  :on-preview="handlePictureCardPreview"
                  :on-remove="handleRemove"
              >
                <el-icon>
                  <Plus/>
                </el-icon>
              </el-upload>
              <el-dialog v-model="dialogVisible">
                <img w-full :src="dialogImageUrl"/>
              </el-dialog>
              <!--  图片上传结束  -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <el-button @click="dialogComplainVisible=false">取消</el-button>
        <el-button type="primary" @click="addWarranty">确定</el-button>
      </template>
   <template>
     <el-steps
         style="max-width: 600px"
         :space="200"
         :active="1"
         finish-status="success"
     >
       <el-step title="Done" />
       <el-step title="Processing" />
       <el-step title="Step 3" />
     </el-steps>
   </template>

    </el-dialog>
  <!-- 报修搜索卡片 -->
  <el-card style="margin:20px;height: 70px;">
    <el-form :inline="true">
      <el-form-item label="报修人">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="search.lname"></el-input>
      </el-form-item>
      <el-form-item label="保修房屋" style="width:290px;">
        <el-input placeholder="请输入内容" style="width:220px;" v-model="search.rhouse"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetRepairs">重置</el-button>
        <el-button type="primary" @click="loadRepairs">查询</el-button>
      </el-form-item>
    </el-form>
  </el-card>
  <!-- 报修列表 -->
  <el-card style="margin:20px;">
    <div style="height: 6vh;background-color:#fff;">
      <span style="line-height:60px;font-size:18px;position:relative;">报修列表</span>
      <el-button type="primary" style="float: right;margin-right: 20px; position:relative;" @click="operatons" >批量删除</el-button>
    </div>
    <el-table :data="tableData"
              ref="multipleTableRef"
              style="width: 100%"
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column prop="rhouse" label="报修房屋" align="center"/>
      <el-table-column prop="rproject" label="维修项目" align="center"/>
      <el-table-column prop="rtitle" label="问题描述" align="center"/>
      <el-table-column prop="lname" label="报修人" align="center" />
      <el-table-column prop="rtime" label="预约日期" align="center"/>
      <el-table-column prop="rstate" label="维修状态" align="center" :formatter="formatStatus"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button link type="primary" size="small"
                     :disabled="scope.row.level==40" @click="auditing(scope.row.rid)">审核</el-button>
          <el-button link type="primary" size="small"
                     :disabled="scope.row.level==40" @click="deleteUser(scope.row.rid)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-card>

  <!-- 审核/查看弹窗 -->
  <el-dialog :title="dialogTitle" v-model="auditDialogVisible">
    <el-form>

      <el-card>
    <el-descriptions direction="horizontal" :column="2" border>
      <el-descriptions-item label="报修房屋">{{ auditDialogData.rhouse }}</el-descriptions-item>
      <el-descriptions-item label="维修项目">{{ auditDialogData.rproject }}</el-descriptions-item>
      <el-descriptions-item label="问题描述">{{ auditDialogData.rtitle }}</el-descriptions-item>
      <el-descriptions-item label="报修人">{{ auditDialogData.lname }}</el-descriptions-item>
      <el-descriptions-item label="预约时间">{{ auditDialogData.rtime }}</el-descriptions-item>
    </el-descriptions>
      </el-card>
      <el-card>
          <el-form-item label="审核流程"></el-form-item>
          <el-form-item>
            <template #default>
              <el-steps
                  style="min-width: 520px;"
                  :active="active"
                  finish-status="success"
              >
                <el-step title="待审核"/>
                <el-step title="已审核"/>
                <el-step title="待维修"/>
                <el-step title="已维修"/>
              </el-steps>
            </template>
          </el-form-item>
          <el-form label-width="80px" label-position="top">
            <el-form-item label="审核意见">
              <el-input placeholder="请输入审核意见" type="textarea" rows="4" v-model="auditDialogData.rcontent" />
            </el-form-item>
          </el-form>
          <template #footer v-if="auditDialogData=true">
            <el-button @click="auditDialogVisible=false">取消</el-button>
            <el-button type="primary" @click="auditReject">拒绝</el-button>
            <el-button type="primary" @click="auditPass">通过</el-button>
          </template>
      </el-card>
    </el-form>
  </el-dialog>

</template>

<script setup>
import {onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios, {create} from "axios";
import qs from "qs";


//控制新增车辆弹窗是否显示
const dialogComplainVisible = ref(false);
//定义变量控制dialog的标题
const dialogTitle = ref('新建报修');
const auditDialogVisible=ref(false);
onMounted(()=>{
  search.value = {};//清空用户搜索表单数据
  loadRepairs();
  loadLandlord();
})


/**图片上传相关代码开始**/
const fileList = ref([]);
const dialogImageUrl = ref('');
const dialogVisible = ref(false);
//移除图片
const handleRemove = (uploadFile, uploadFiles) => {
  console.log(uploadFile, uploadFiles);
  //1.得到要删除的图片路径 uploadFile就是我们上传的图片文件对象
  let imgs = uploadFile.response.data;
  //2.给后端发请求,删除图片
  axios.post(BASE_URL + '/v1/file/remove?imgs=' + imgs).then((response) => {
    if (response.data.code == 2000) {
      ElMessage.success('删除成功!');
    }
  })
}
//图片上传后预览
const handlePictureCardPreview = (uploadFile) => {
  dialogImageUrl.value = uploadFile.url;
  dialogVisible.value = true;
  console.log(uploadFile);
}
/**图片上传相关代码结束**/

//报修列表数据数组
const tableData=ref([]);
let multipleSelection=ref([]);
let ids=ref([]);
const handleSelectionChange = (val) => {
  /*multipleSelection.value = val
  console.log(multipleSelection.value);*/
  multipleSelection=[];
  val.forEach(item => {
    multipleSelection.push(item.rid);
    console.log(item.rid);
  })
}
const operatons = () => {
  if (Array.isArray(multipleSelection)) {
    multipleSelection.forEach(ids => {
      //deleteUser({rid})
      axios.post(BASE_URL + '/v1/community/delete/' + ids).then((response) => {
        if (response.data.code == 2000) {
          ElMessage.success('删除成功!');
          loadRepairs();
        } else {
          ElMessage.error(response.data.msg);
        }
      })
    })
    multipleSelection = [];
  }else{
    ElMessage.error('请选择要删除的报修!');
  }
}

const deleteUser = (rid)=>{
  if (confirm('确定删除该报修吗?')) {
    axios.post(BASE_URL + '/v1/community/delete/' + rid).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadRepairs();
        console.log(response.data.data);
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}

//报修用户数组
const repairerOptions=ref([]);
const loadLandlord = () => {
  axios.get(BASE_URL+'/v1/workspace/select').then((response)=>{
    if (response.data.code==2000){
      repairerOptions.value = response.data.data;
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}




const search=ref({
  rhouse:'',
  rproject:'',
  rtitle:'',
  lname:'',
  rlid:'',
  rtime:'',
  rstate:'',
  imgs:''
})
const loadRepairs = () => {
  let data =qs.stringify(search.value);
  console.log(data);
  axios.get(BASE_URL+'/v1/community/selectlist?'+data).then((response)=>{
    if (response.data.code==2000){
      tableData.value=response.data.data;
      console.log(response.data.data);
    }else {
      ElMessage.error(response.data.msg);
    }
  })
  //console.log(tableData.value);
}


const resetRepairs = ()=>{
  search.value = {};//清空用户搜索表单数据
  loadRepairs();//重新加载用户数据
}
const auditDialogData=ref({
  rhouse:'',
  rproject:'',
  rtitle:'',
  lname:'',
  rtime:'',
  rstatus:'',
  rcontent:''
});
//审批报修
const auditing = (rid) => {
  auditDialogVisible.value = true;
  axios.get(BASE_URL+'/v1/community/selectlist?rid='+rid).then((response)=>{
    if (response.data.code==2000){
      auditDialogData.value = response.data.data[0];
    }else{
      ElMessage.error(response.data.msg);
    }
  })
}
//新增报修
const addWarranty = () => {
  search.value.imgs=fileList.value[0].response.data;
  let data =qs.stringify(search.value);
  console.log(data);
  axios.post(BASE_URL+'/v1/community/add',data).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('添加成功!');
      dialogComplainVisible.value=false;
      loadRepairs();
      search.value = {};
    }else {
      ElMessage.error(response.data.msg);
    }
  })
  loadRepairs();
  search.value = {};
}

/*const deleteUser = (rid)=>{
  if (confirm('确定删除该报修吗?')) {
    axios.post(BASE_URL + '/v1/community/delete/' + rid).then((response) => {
      if (response.data.code == 2000) {
        ElMessage.success('删除成功!');
        loadRepairs();
        console.log(response.data.data);
      } else {
        ElMessage.error(response.data.msg);
      }
    })
  }
}*/
const repairStatusOptions=ref([
  {value:'10',label:'待审核'},
  {value:'20',label:'已审核'},
  {value:'30',label:'待维修'},
  {value:'40',label:'已维修'},
]);
const formatStatus = (row,column,cellValue,index)=>{
  if (cellValue==10){
    cellValue = '待审核';
  }else if (cellValue==20){
    cellValue = '已审核';
  }else if (cellValue==30){
    cellValue = '待维修';
  }else if (cellValue==40){
    cellValue = '已维修';
  }
  return cellValue;
}
//步骤条激活
const active=ref(0);
const auditReject = () => {

}
//审核通过
const auditPass = (rid) => {
  axios.post(BASE_URL+'/v1/community/update/'+rid).then((response)=>{
    if (response.data.code==2000){
      ElMessage.success('审核成功!');
      auditDialogVisible.value=false;
      loadRepairs();
      console.log(response.data.data)
    }else {
      ElMessage.error(response.data.msg);
    }
  })
}
</script>